package com.contactmgr.utils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;

import com.contactmgr.logObjects.CallDataLog;

public class CallLogReader {

	public static List<CallDataLog> getAllCallLogs(ContentResolver cr) {
//		String[] strFields = { android.provider.CallLog.Calls.NUMBER, android.provider.CallLog.Calls.TYPE,
//				android.provider.CallLog.Calls.CACHED_NAME, android.provider.CallLog.Calls.CACHED_NUMBER_TYPE };
		String strOrder = android.provider.CallLog.Calls.DATE + " DESC";
		Uri callUri = Uri.parse("content://call_log/calls");
		// android.provider.CallLog.Calls.CONTENT_URI,
		Cursor cur = cr.query(callUri, null, null, null, strOrder);
		List<CallDataLog> callList = new ArrayList<CallDataLog>();
		// loop through cursor
		while (cur.moveToNext()) {
			CallDataLog callLog = new CallDataLog();
			String callNumber = cur.getString(cur.getColumnIndex(android.provider.CallLog.Calls.NUMBER));
//			String callName = cur.getString(cur.getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME));
			String callDate = cur.getString(cur.getColumnIndex(android.provider.CallLog.Calls.DATE));
			String callType = cur.getString(cur.getColumnIndex(android.provider.CallLog.Calls.TYPE));
//			String isCallNew = cur.getString(cur.getColumnIndex(android.provider.CallLog.Calls.NEW));
			String duration = cur.getString(cur.getColumnIndex(android.provider.CallLog.Calls.DURATION));
			if (Integer.parseInt(callType) == android.provider.CallLog.Calls.MISSED_TYPE) {
				callLog.setType(CallDataLog.TYPE.MISSED);
			}
			if (Integer.parseInt(callType) == android.provider.CallLog.Calls.OUTGOING_TYPE) {
				callLog.setType(CallDataLog.TYPE.OUTGOING);
			}
			if (Integer.parseInt(callType) == android.provider.CallLog.Calls.INCOMING_TYPE) {
				callLog.setType(CallDataLog.TYPE.INCOMING);
			}
			callLog.setDate(callDate);
			// log.setDate(date);
			callLog.setNumber(callNumber);
			callLog.setDuration(Integer.parseInt(duration));
			callList.add(callLog);
		}
		return callList;
	}

	public static List<CallDataLog> getMissedCallLogs(ContentResolver cr) {
		List<CallDataLog> allCallData = getAllCallLogs(cr);
		return getMissedCallLogs(allCallData);
	}

	public static List<CallDataLog> getIncomingCallLogs(ContentResolver cr) {
		List<CallDataLog> allCallData = getAllCallLogs(cr);
		return getIncomingCallLogs(allCallData);
	}

	public static List<CallDataLog> getOutGoingCallLogs(ContentResolver cr) {
		List<CallDataLog> allCallData = getAllCallLogs(cr);
		return getOutGoingCallLogs(allCallData);
	}

	public static List<CallDataLog> getMissedCallLogs(List<CallDataLog> allCallData) {
		List<CallDataLog> missedCallList = new ArrayList<CallDataLog>();
		for (CallDataLog curCallData : allCallData) {
			if (curCallData.getType().equals(CallDataLog.TYPE.MISSED)) {
				missedCallList.add(curCallData);
			}
		}
		return missedCallList;
	}

	public static List<CallDataLog> getIncomingCallLogs(List<CallDataLog> allCallData) {
		List<CallDataLog> incomingCallList = new ArrayList<CallDataLog>();
		for (CallDataLog curCallData : allCallData) {
			if (curCallData.getType().equals(CallDataLog.TYPE.INCOMING)) {
				incomingCallList.add(curCallData);
			}
		}
		return incomingCallList;
	}

	public static List<CallDataLog> getOutGoingCallLogs(List<CallDataLog> allCallData) {
		List<CallDataLog> outGoingCallList = new ArrayList<CallDataLog>();
		for (CallDataLog curCallData : allCallData) {
			if (curCallData.getType().equals(CallDataLog.TYPE.OUTGOING)) {
				outGoingCallList.add(curCallData);
			}
		}
		return outGoingCallList;
	}

}
